﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace h2100
{
    /// <summary>
    /// Làm nhiệm vụ xuất văn bản
    /// </summary>
    public class hText    
	{
		private SpriteFont _fontBold;
        private SpriteFont _fontBoldMedium;
        private SpriteFont _fontRegular;
        		
		public hText(ContentManager content)
		{
			_fontBold = content.Load<SpriteFont>(hGlobal.PathGui+hGlobal.FontBold);
            _fontRegular = content.Load<SpriteFont>(hGlobal.PathGui + hGlobal.FontRegular);
            _fontBoldMedium = content.Load<SpriteFont>(hGlobal.PathGui + hGlobal.FontBoldMedium);
		}

        /// <summary>
        /// In chuỗi canh giữa vùng chữ nhật
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="spriteBatch"></param>
        /// <param name="x">x góc trái trên</param>
        /// <param name="y">y góc trái trên</param>
        /// <param name="w">chiều rộng hình chữ nhật</param>
        /// <param name="h">chiều cao hình chữ nhật</param>
        /// <param name="str">Chuỗi cần in</param>
        /// <param name="color">Màu</param>
        /// <param name="style">In thường, in đậm</param>
        public void print(GameTime gameTime, SpriteBatch spriteBatch, float x, float y, float w, float h, String str, Color color, hGlobal.FontStyle style)
		{
            // Vị trí ép về kiểu int để tránh chữ bị vỡ
            int posX,posY;

            switch (style)
            {
                case hGlobal.FontStyle.Bold:
                    posX = (int)((x + w / 2) - _fontBold.MeasureString(str).X / 2);
                    posY = (int)((y + h / 2) - _fontBold.MeasureString(str).Y / 2);
                    spriteBatch.DrawString(_fontBold, str, new Vector2(posX, posY), color);
                    break;
                case hGlobal.FontStyle.Regular:
                    posX = (int)((x + w / 2) - _fontRegular.MeasureString(str).X / 2);
                    posY = (int)((y + h / 2) - _fontRegular.MeasureString(str).Y / 2);
                    spriteBatch.DrawString(_fontRegular, str, new Vector2(posX, posY), color);
                    break;
                case hGlobal.FontStyle.BoldMedium:
                    posX = (int)((x + w / 2) - _fontBoldMedium.MeasureString(str).X / 2);
                    posY = (int)((y + h / 2) - _fontBoldMedium.MeasureString(str).Y / 2);
                    spriteBatch.DrawString(_fontBoldMedium, str, new Vector2(posX, posY), color);
                    break;
                default: //Mặc định dùng bold
                    posX = (int)((x + w / 2) - _fontBold.MeasureString(str).X / 2);
                    posY = (int)((y + h / 2) - _fontBold.MeasureString(str).Y / 2);
                    spriteBatch.DrawString(_fontBold, str, new Vector2(posX, posY), color);
                    break;
            }
            
		}
    }
}
